package com.zhuguowei.chapter_2_4.provider;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.zhuguowei.chapter_2_4.R;
import com.zhuguowei.chapter_2_4.aidl.Book;
import com.zhuguowei.chapter_2_4.model.User;

public class ProviderActivity extends AppCompatActivity {
    private static final String TAG = "ProviderActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider);
        Uri bookUri = Uri.parse("content://com.zhuguowei.chapter_2_4.provider/book");
        //插入一本新书 zgw-2017/12/5 13:56
        ContentValues values = new ContentValues();
        values.put("_id",6);
        values.put("name","程序开发艺术");
        getContentResolver().insert(bookUri,values);
        //查询 zgw-2017/12/5 13:57
        Cursor bookCursor = getContentResolver().query(bookUri, new String[]{"_id", "name"}, null, null, null);
        while(bookCursor.moveToNext()) {
            Book book = new Book();
            book.setBookID(bookCursor.getInt(0));
            book.setBookName(bookCursor.getString(1));
            Log.e(TAG, "onCreate: book=" + book.toString() );
        }
        bookCursor.close();

        Uri userUri = Uri.parse("content://com.zhuguowei.chapter_2_4.provider/user");
        Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "sex"}, null, null, null);
        while (userCursor.moveToNext()) {
            User user = new User();
            user.userId = userCursor.getInt(0);
            user.userName = userCursor.getString(1);
            user.isMale = userCursor.getInt(2) == 1;
            Log.e(TAG, "onCreate: user=" + user.toString() );
        }
        userCursor.close();
    }



}
